package com.lesso.cyg.sunflower.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.cyg.sunflower.core.ApiResponse;
import com.lesso.cyg.sunflower.pojo.dto.AttendanceRuleDTO;
import com.lesso.cyg.sunflower.pojo.dto.AttendanceRuleQueryDTO;
import com.lesso.cyg.sunflower.service.IAttendanceRuleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 考勤规则表 前端控制器
 * </p>
 *
 * @author cyg
 * @since 2025-07-02
 */
@RestController
@RequestMapping("/base/attendance/rule")
public class AttendanceRuleController {
    @DubboReference
    private IAttendanceRuleService attendanceRuleService;

    @PostMapping
    public ApiResponse<Integer> save(@Valid @RequestBody AttendanceRuleDTO dto) {
        return ApiResponse.ok("success", attendanceRuleService.save(dto));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Object> delete(@PathVariable("id") Long id) {
        Assert.notNull(id, "id不能为空");
        if (id < 0) {
            throw new RuntimeException("id不能为空");
        }
        attendanceRuleService.deleteById(id);
        return ApiResponse.ok();
    }

    @PutMapping
    public ApiResponse<Integer> update(@Valid @RequestBody AttendanceRuleDTO dto) {
        return ApiResponse.ok("success", attendanceRuleService.update(dto));
    }

    @GetMapping("/page")
    public ApiResponse<Page<AttendanceRuleDTO>> page(AttendanceRuleQueryDTO dto) {
        return ApiResponse.ok("success", attendanceRuleService.page(dto));
    }
}